<%@Page Language="VB" %>
<%@Import Namespace="System.XML" %>

<html><head>
<title>Accessing XML documents using the DOM</title>
</head>
<body bgcolor="#ffffff">
<span class="heading">Accessing XML documents using the DOM</span><hr />

<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)

   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "booklist.xml"  
   Dim objXMLDoc As New XMLDocument()

   Try
     objXMLDoc.Load(strXMLPath)
      outDocURL.innerHTML = "Loaded file: <b>" & strXMLPath & "</b>"

   Catch objError As Exception

     outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try
   Dim strNodes As String
   outResults.innerHTML = strNodes & GetChildNodes(objXMLDoc.ChildNodes, 0)

End Sub


Function GetChildNodes(objNodeList As XMLNodeList, intLevel As Integer) As String

  Dim strNodes As String = ""
  Dim objNode As XMLNode
  Dim objAttr As XMLAttribute


  For Each objNode In objNodeList

     strNodes = strNodes & GetIndent(intLevel) _
              & GetNodeType(objNode.NodeType) & ": <b>" & objNode.Name

     If objNode.NodeType = XMLNodeType.XmlDeclaration Then
        Dim objXMLDec =CType(objNode, XmlDeclaration)
        strNodes = strNodes & "</b>&nbsp; version=<b>" & objXMLDec.Version _
                 & "</b>&nbsp; standalone=<b>" & objXMLDec.Standalone & "</b><br />"
     Else
        strNodes = strNodes & "</b>&nbsp; value=<b>" & objNode.Value & "</b><br />"
     End If
     If objNode.NodeType = XMLNodeType.Element Then
        For Each objAttr In objNode.Attributes
           strNodes +=GetIndent(intLevel + 1) _
                    & GetNodeType(objAttr.NodeType) & ": <b>" & objAttr.Name _
                    & "</b>&nbsp; value=<b>" & objAttr.Value & "</b><br />"
        Next
     End If

     If objNode.HasChildNodes Then
      strNodes = strNodes & GetChildNodes(objNode.childNodes, intLevel + 1)
     End If

  Next  

  Return strNodes  

End Function


Function GetIndent(intLevel As Integer)
  Dim strIndent As String = ""
  Dim intIndent As Integer
  For intIndent = 0 To intLevel
    strIndent = strIndent & "&nbsp; &nbsp; &nbsp; "
  Next
  Return strIndent
End Function


Function GetNodeType(intType As Integer) As String
   Select Case (intType)
     Case 0: Return "NONE"
     Case 1: Return "ELEMENT"
     Case 2: Return "ATTRIBUTE"
     Case 3: Return "TEXT"
     Case 4: Return "CDATA SECTION"
     Case 5: Return "ENTITY REFERENCE"
     Case 6: Return "ENTITY"
     Case 7: Return "PROCESSING INSTRUCTION"
     Case 8: Return "COMMENT"
     Case 9: Return "DOCUMENT"
     Case 10: Return "DOCUMENT TYPE"
     Case 11: Return "DOCUMENT FRAGMENT"
     Case 12: Return "NOTATION"
     Case 13: Return "WHITESPACE"
     Case 14: Return "SIGNIFICANT WHITESPACE"
     Case 15: Return "END ELEMENT"
     Case 16: Return "END ENTITY"
     Case 17: Return "XML DECLARATION"
     Case 18: Return "NODE (ALL)"
     Case Else: Return "UNKNOWN"
   End Select
End Function
</script>
</body>
</html>
